<?php
    class GP_hooks 
    {
        public $enabled 		= false;
        public $hooks   		= array();
        public $in_progress	    = false;

        /**
         * Constructor
         *
         */
        function __construct()
        {
            if (false === HOOK_ENABLED)
                return;
            $this -> enabled = true;
        }
        
        /**
         * addHook
         *
         * @access	private
         * @param	when	    (pre | post [system, controller, view])
         * @param	function	function to execute
         * @param	params  	params to allocate to function
         * @return	void
         */  
        
        function addHook($when, $function, $params)
        {
            if (false === $this -> enabled)
                return;
           
            $hooks = getvar('__hooks__');
            $md5 = md5($when . $function . $params);
           
            if (null === $hooks)
                $hooks = array();
            else
                if (true === $hooks[$md5])
                    return;
            $hooks[$md5] = true;
            $hooks[$when][] = array(
                'function' => $function, 
                'params' => $params
            );
            setvar('__hooks__', $hooks);
        }
      
        // --------------------------------------------------------------------

        /**
         * Run
         *
         * @access	private
         * @return	void
         */  
        function run()
        {
            // If hooks are not enabled in the config file
            // there is nothing else to do

            if (false === $this -> enabled)
                return;

            // Grab the "hooks" definition file.
            // If there are no hooks, we're done.

            $hooks = getvar('__hooks__');

            if (null === $hooks)
                return;

            $this -> hooks = $hooks;
        }
      
        // --------------------------------------------------------------------

        /**
         * Call Hook
         *
         * Calls a particular hook
         *
         * @access	private
         * @param	string	the hook name
         * @return	mixed
         */
        function _call_hook($when = '')
        {
            if (false === $this -> enabled || !isset($this -> hooks[$when]))
                return false;

            if (isset($this -> hooks[$when][0]) && is_array($this -> hooks[$when][0]))
                foreach ($this -> hooks[$when] as $val)
                    $this -> _run_hook($val);
            else
                $this -> _run_hook($this -> hooks[$when]);

            return true;
        }

        // --------------------------------------------------------------------

        /**
         * Run Hook
         *
         * Runs a particular hook
         *
         * @access	private
         * @param	array	the hook details
         * @return	bool
         */
        function _run_hook($data)
        {
            if (!is_array($data) || false === $this -> enabled)
                return false;

            // -----------------------------------
            // Safety - Prevents run-away loops
            // -----------------------------------

            // If the script being called happens to have the same
            // hook call within it a loop can happen

            if (true === $this -> in_progress)
                return;
            
            $function = $data['function'];
            $params = $data['params'];

            if (!function_exists($function))
                return false;
            $function($params);
            
            $this -> in_progress = false;
            return true;
        }

    }
?>